package com.semidata.trp.material.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;

import java.util.StringTokenizer;

import com.semidata.rtd.core.http.server.HttpDispatchHandler;
import com.semidata.rtd.core.http.server.HttpDispatcher;
import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.material.config.Config;

public class MaterialHttpDispatcher implements HttpDispatcher {

    private static String normalizePrefix(String prefix) {
       
        int start = prefix.length();

        for (int i = 0; i < prefix.length(); i++) {
            if (prefix.charAt(i) == '/')
                continue;
            start = i;
            break;
        }

        int end = start - 1;

        for (int i = prefix.length() - 1; i >= start; i--) {
            if (prefix.charAt(i) == '/')
                continue;
            end = i;
            break;
        }

        return end < start ? "" : prefix.substring(start, end + 1);
       
    }
    
    public HttpDispatchHandler getDispatchHandler(ChannelHandlerContext ctx, HttpRequest request) {

        Config config = Config.getConfig();

        StringTokenizer st = new StringTokenizer(request.getUri(), "/");
        String prefix = normalizePrefix(config.getUrlPrefix());

        String objName;
        String methodName;
        String content;
        
        if (prefix.isEmpty()) {
            if (st.countTokens() != 2) {
                Logger.log("ERROR", "Url is Illegal, " + request.getUri());
                return null;
            }
            
            objName = "";
            methodName = st.nextToken();
            content = st.nextToken();
        } else {
            
            if (st.countTokens() != 3) {
                Logger.log("ERROR", "Url is Illegal, " + request.getUri());
                return null;
            }
            
            objName = st.nextToken();
            methodName = st.nextToken();
            content = st.nextToken();
            if (!objName.equalsIgnoreCase(prefix)) {
                Logger.log("ERROR", "upload format is host/Material/upload/png|gif|bmp|jpg, ERROR URI : " + request.getUri());
                return null;
            }
        }
        
        if (methodName.equalsIgnoreCase("upload")) {

            if (!config.getAvailableImageTypes().containsKey(content)) {
                Logger.log("ERROR", "upload format is host/Material/upload/png|gif|bmp|jpg, ERROR URI : " + request.getUri());
                return null;
            }
            return new MaterialUploadHttpHandler(ctx, request, content);

        } else if (methodName.equalsIgnoreCase("remove")) {
            return new MaterialRemoveHttpHandler(ctx, request, content);

        } else if (methodName.equalsIgnoreCase("review")) {

            int pos = content.lastIndexOf('.');
            if (pos == -1) {
                Logger.log("ERROR", "Url is Illegal, " + request.getUri());
                return null;
            }

            String filename = content.substring(0, pos);
            String type = content.substring(pos + 1);

            if (filename.length() != 32 || !config.getAvailableImageTypes().containsKey(type)) {
                Logger.log("ERROR", "Url is Illegal, " + request.getUri());
                return null;
            }

            return new MaterialReviewHttpHandler(ctx, request, filename, type);

        } else {
            Logger.log("ERROR", "upload format is host/Material/upload/png|gif|bmp|jpg, ERROR URI : " + request.getUri());
            return null;
        }

    }

}
